##
##	mydns.m4
## autoconf macros for the MyDNS package.
##
##	Copyright (C) 2002  Don Moore <bboy@bboy.net>
##
##	$Header: /pub/cvsroot/mydns/m4/mydns.m4,v 1.9 2002/07/10 05:47:32 bboy Exp $
##

##
## Check for IPv6 support
##
AC_DEFUN([AC_CHECK_IPV6],
	[
		AC_MSG_CHECKING([whether IPv6 is supported])
		AC_TRY_COMPILE([#include <netinet/in.h>], [struct in6_addr addr; memset(&addr,0,sizeof(struct in6_addr));],
			[ AC_DEFINE([HAVE_IPV6], 1, [Is IPv6 supported?])
			  AC_MSG_RESULT([yes]) ],
			AC_MSG_RESULT([no]))
	]
)
 
##
## Let --enable-mtrace enable support for mtrace() malloc debugging
##
AC_DEFUN([AC_ENABLE_MTRACE],
	[
		AC_MSG_CHECKING([whether to include malloc debugging support])
		AC_ARG_ENABLE(mtrace,
			AC_HELP_STRING([--enable-mtrace], [enable malloc debugging via mtrace()]),
			ac_mtrace_support=yes,ac_mtrace_support=no)
		AC_MSG_RESULT([$ac_mtrace_support])

		if test "$ac_mtrace_support" = yes
		then
			AC_CHECK_FUNC(mtrace,
				AC_DEFINE([MTRACE_ENABLED], 1, [Enable mtrace() for malloc debugging]),
				AC_MSG_ERROR([--enable-mtrace specified but mtrace() function not found.]))
			AC_CHECK_HEADERS([mcheck.h])
		fi
	]
)

##
##	Make --enable-debug turn on debugging
##
AC_DEFUN([AC_ENABLE_DEBUG],
	[
		AC_MSG_CHECKING([whether to enable debugging])
		AC_ARG_ENABLE(debug,
			AC_HELP_STRING([--enable-debug], [enable debugging options]),
			[
				if test "$enableval" = yes
				then
					AC_DEFINE(DEBUG, 1, [Compile with support for debugging options?])
					AC_MSG_RESULT([yes])
				else
					AC_MSG_RESULT([no])
				fi
			], AC_MSG_RESULT([no]))
	]
)

##
## Find zlib compression library (@LIBZ@)
##
AC_DEFUN([AC_LIB_Z],
	[
		ac_mydns_lib_dirs="/lib /usr/lib /usr/local/lib"
		AC_ARG_WITH(zlib,
			AC_HELP_STRING([--with-zlib=DIR],
								[look for the zlib compression library in DIR]),
			ac_mydns_lib_dirs="$withval $ac_mydns_lib_dirs")
		ac_mydns_lib_found=no, ac_mydns_lib_ok=no
		for dir in $ac_mydns_lib_dirs
		do
			if test "$ac_mydns_lib_found" != yes
			then
				AC_CHECK_FILE($dir/libz.a, ac_mydns_lib_found=yes, ac_mydns_lib_found=no)
				if test "$ac_mydns_lib_found" = yes
				then
					AC_CHECK_LIB(z, deflate, ac_mydns_lib_ok=yes, ac_mydns_lib_ok=no)
					test "$ac_mydns_lib_ok" = yes && LIBZ="-L$dir -lz"
				fi
			fi
		done
		if test "$ac_mydns_lib_ok" != yes
		then
			AC_MSG_ERROR([MySQL client library not found.  (See config.log for more information.)])
		fi
		AC_SUBST(LIBZ)
	]
)

##
## Find MySQL client library (@LIBMYSQLCLIENT@)
##
AC_DEFUN(AC_LIB_MYSQLCLIENT,
	[
		ac_mydns_lib_dirs="/lib /usr/lib /usr/lib/mysql /usr/local/lib \
								 /usr/local/lib/mysql /usr/local/mysql/lib"
		AC_ARG_WITH(mysql-lib,
			AC_HELP_STRING([--with-mysql-lib=DIR],
								[look for the MySQL client library in DIR]),
			ac_mydns_lib_dirs="$withval $ac_mydns_lib_dirs")
		ac_mydns_lib_found=no, ac_mydns_lib_ok=no
		ac_mydns_libs="$LDFLAGS"
		for dir in $ac_mydns_lib_dirs
		do
			if test "$ac_mydns_lib_found" != yes
			then
				AC_CHECK_FILE($dir/libmysqlclient.a, ac_mydns_lib_found=yes, ac_mydns_lib_found=no)
				if test "$ac_mydns_lib_found" = yes
				then
					LIBS="$ac_mydns_libs -L$dir -lmysqlclient $LIBZ"
					AC_CHECK_LIB(mysqlclient, mysql_real_connect, ac_mydns_lib_ok=yes, ac_mydns_lib_ok=no)
					LIBS="$ac_mydns_libs"
					test "$ac_mydns_lib_ok" = yes && LIBMYSQLCLIENT="-L$dir -lmysqlclient"
				fi
			fi
		done
		if test "$ac_mydns_lib_ok" != yes
		then
			AC_MSG_ERROR([MySQL client library not found.  (See config.log for more information.)])
		fi
		AC_SUBST(LIBMYSQLCLIENT)
	]
)

##
##	Find location of MySQL header files (@MYSQL_INCLUDE@)
##
AC_DEFUN(AC_HEADER_MYSQL,
	[
		ac_mydns_header_dirs="/usr/include /usr/include/mysql /usr/local/include \
									 /usr/local/include/mysql /usr/local/mysql/include"
		ac_mydns_header_found=no, ac_mydns_header_ok=no
		AC_ARG_WITH(mysql-include,
			AC_HELP_STRING([--with-mysql-include=DIR],
								[look for MySQL include files in DIR]),
			ac_mydns_header_dirs="$withval $ac_mydns_header_dirs")
		for dir in $ac_mydns_header_dirs
		do
			if test "$ac_mydns_header_found" != yes
			then
				AC_CHECK_FILE($dir/mysql.h, ac_mydns_header_found=yes, ac_mydns_header_found=no)
				if test "$ac_mydns_header_found" = yes
				then
					AC_CHECK_HEADER($dir/mysql.h, ac_mydns_header_found=yes, ac_mydns_header_found=no)
					test "$ac_mydns_header_found" = yes && MYSQL_INCLUDE="-I$dir"
				fi
			fi
		done
		if test "$ac_mydns_header_found" != yes
		then
			AC_MSG_ERROR([Could not find mysql.h.  (See config.log for more information.)])
		fi
		AC_SUBST(MYSQL_INCLUDE)
	]
)

##
## Set better CFLAGS for known system types
##
AC_DEFUN(AC_MYDNS_CFLAGS,
	[
		test "$CFLAGS" = "-g -O2" -o "$CFLAGS" = "-O2" && ac_mydns_default_cflags=yes

		AC_MSG_CHECKING([whether to attempt to optimize CFLAGS])
		AC_ARG_WITH(cflags,
			AC_HELP_STRING([--without-cflags],
								[do not attempt to optimize CFLAGS]),
					ac_mydns_cflags="$withval", ac_mydns_cflags=yes)

		if test "$ac_mydns_default_cflags" = yes -a "$ac_mydns_cflags" = yes
		then
			ac_mydns_opt_cflags=yes
		else
			ac_mydns_opt_cflags=no
		fi
		AC_MSG_RESULT($ac_mydns_opt_cflags)

		if test "$ac_mydns_opt_cflags" = yes
		then
			ac_mydns_systype=`$ac_config_guess`
			case "$ac_mydns_systype" in
				i686-pc-linux-gnu)
					CFLAGS="-O3 -Wall"
					CFLAGS="$CFLAGS -march=i686"
					CFLAGS="$CFLAGS -fomit-frame-pointer"
#					CFLAGS="$CFLAGS -funroll-loops"
#					CFLAGS="$CFLAGS -finline-functions"
					CFLAGS="$CFLAGS -ffast-math"
					CFLAGS="$CFLAGS -funsigned-char"
					;;

				*) ;;
			esac
		fi
	]
)

##
## Set some extra variables to make changes easier
##
AC_DEFUN(AC_MYDNS_PKGINFO,
	[
		PACKAGE_HOMEPAGE="http://mydns.bboy.net"
		PACKAGE_COPYRIGHT="Copyright (C) 2002 Don Moore"
		PACKAGE_AUTHOR="Don Moore <bboy@bboy.net>"
		PACKAGE_DATE=`date +"%b %Y"`
		## XXX: Make this configurable
		MYDNS_CONF="/etc/$PACKAGE_NAME.conf"

		AC_SUBST(PACKAGE_DATE)
		AC_DEFINE_UNQUOTED(PACKAGE_DATE, ["$PACKAGE_DATE"], [Package build date])

		AC_SUBST(PACKAGE_HOMEPAGE)
		AC_DEFINE_UNQUOTED(PACKAGE_HOMEPAGE, ["$PACKAGE_HOMEPAGE"], [This package's homepage])

		AC_SUBST(PACKAGE_COPYRIGHT)
		AC_DEFINE_UNQUOTED(PACKAGE_COPYRIGHT, ["$PACKAGE_COPYRIGHT"], [Copyright holder])

		AC_SUBST(PACKAGE_AUTHOR)
		AC_DEFINE_UNQUOTED(PACKAGE_AUTHOR, ["$PACKAGE_AUTHOR"], [This package's primary author])

		AC_SUBST(MYDNS_CONF)
		AC_DEFINE_UNQUOTED(MYDNS_CONF, ["$MYDNS_CONF"], [Default config file location])
	]
)

## vi:set ts=3:
